package com.shopping.huaju.commodity.service.impl;

import com.shopping.huaju.commodity.dao.BrandDao;
import com.shopping.huaju.commodity.dao.CategoryDao;
import com.shopping.huaju.commodity.entity.BrandEntity;
import com.shopping.huaju.commodity.entity.CategoryEntity;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.Query;

import com.shopping.huaju.commodity.dao.CategoryBrandRelationDao;
import com.shopping.huaju.commodity.entity.CategoryBrandRelationEntity;
import com.shopping.huaju.commodity.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean saveCategoryBrandRelationEntity(CategoryBrandRelationEntity categoryBrandRelation) {
        //categoryBrandRelation 前端传来的 对象 只包含 brandId 和 categoryId
        //需要 将categoryBrandRelation其他字段补充完整，再入库
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCategoryId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCategoryName(categoryEntity.getName());
        //将完整的 categoryBrandRelation 入库
        return this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> getBrandsByCategoryId(Long categoryId) {
        // 根据 categoryId 获取与该分类关联的品牌的id列表
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities
                = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", categoryId));
        List<Long> brandIds = categoryBrandRelationEntities.stream()
                .map(item -> item.getBrandId()).collect(Collectors.toList());
        //判断
        if(brandIds.isEmpty()){
            //如果brandIds为空 则该分类没有对应的品牌关联，直接返回空的list
            return Collections.emptyList();
        }
        //如果 brandId 有值，获取brand信息进行返回
        List<BrandEntity> brandEntities = brandDao.selectBatchIds(brandIds);
        //返回
        return brandEntities;
    }


}